package com.ymy.thinking.in.spring.dependency.lookup;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;

/**
 * {@link BeanCreationException} 示例
 *
 * @author Ringo
 * @date 2021/9/10 14:39
 */
public class BeanCreationExceptionDemo {

    public static void main(String[] args) {
        // 创建 IoC 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 注册 BeanDefinition
        // Bean Class POJO 是一个普通类, 实现了 InitializingBean, 不过初始化方法中抛出了异常！
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(POJO.class);
        applicationContext.registerBeanDefinition("pojo", builder.getBeanDefinition());

        // 开启 Spring 应用上下文
        applicationContext.refresh();

        // 关闭 Spring 应用上下文
        applicationContext.close();
    }

    public static class POJO implements InitializingBean {

        @PostConstruct
        public void init() throws Exception {
            throw new Exception("init(): throw exception...");
        }

        @Override
        public void afterPropertiesSet() throws Exception {
            throw new Exception("afterPropertiesSet(): throw exception...");
        }
    }

}
